<!--
 * @lastUpdateBy : 张瀚
 * @description: 本地调试使用的登录弹窗
-->
<template>
  <!--本地调试用的登录弹窗-->
  <el-dialog title="登录" :visible="true" :show-close="false">
    <el-row type="flex" align="middle">
      <el-col :span="24" style="margin-bottom: 15px">
        <el-input size="small" placeholder="请输入用户名" v-model="debugUserName" />
      </el-col>
      <el-col :span="24" style="margin-bottom: 15px">
        <el-input size="small" type="password" placeholder="请输入密码" v-model="debugPassword" />
      </el-col>
      <el-col :span="20"> </el-col>
      <el-col :span="4">
        <el-button size="small" type="primary" @click="login">登录</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
//登录接口要用到的文件
import sm3 from "../../../public/agcloud-sdk/js/sm3";
import sm4 from "../../../public/agcloud-sdk/js/sm4";
import md5 from "../../../public/agcloud-sdk/js/md5";
export default {
  components: {},
  props: {},
  data() {
    return {
      showDebugLoginDialog: false,
      debugUserName: "",
      debugPassword: "",
    };
  },
  created() {},
  mounted() {},
  destroyed() {},
  computed: {},
  watch: {},
  methods: {
    //返回当前是否应该是调试模式，如果是就显示弹窗，隐藏主界面
    isDebug() {
      return self == top;
      //不是嵌入iframe且开头是http://localhost或者127.0.0.1的是调试模式
      // return self == top && (window.location.href.startsWith("http://localhost") || window.location.href.startsWith("http://127.0.0.1"));
    },
    open() {
      //先检查token失效没有，没有的话就不用重新登录了,tooken的生命周期，一般一天左右过期
      let tookenLife = Date.now() - (Number.parseInt(localStorage.getItem("token_getTime")) || Date.now());
      //半天内不用重新登录，直接返回
      if (0 < tookenLife && tookenLife < 12 * 3600 * 1000) {
        //有token，默认登录成功，至于token无效等情况，在接口调用后会退出到登录界面
        this.$emit("showRouter");
        return;
      }
      //不是嵌在iframe的是调试模式，尝试登陆获取token
      this.showDebugLoginDialog = true;
    },
    login() {
      agcim.net
        .http({
          method: "POST",
          url: agcim.net.apiPath.login,
          params: {
            isApp: true,
            autoLogin: true,
            deviceType: sm4("pc"),
            username: sm4(this.debugUserName),
            password: sm3(md5(this.debugPassword)),
            proPassword: sm3(sm4(this.debugPassword)),
          },
          headers: {
            //注意这个值，是从后台获取的应用id的base64编码，可能会变的
            Authorization: agcim.net.apiPath._loginAuth,
            "Content-Type": "application/x-www-form-urlencoded",
          },
        })
        .then((data) => {
          if (data.success != true) {
            //登陆失败
            this.$message.error("调试登陆失败，具体请看控制台错误信息！");
            console.error(data);
            return;
          }
          this.showDebugLoginDialog = false;
          localStorage.setItem("access_token", data.content.access_token)
          //登陆成功
          this.$emit("finishLogin", data.content.opusLoginUser.user);
          console.log('登录成功',data)
        });
    },
  },
};
</script>
<style lang="scss" module="myStyle"></style>
<style lang="scss" scoped></style>
